<?php

namespace App\Models\Entity\content_audit;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Entity\Asset\Video\Vasseti;

/**
 * Class content_audit
 * @package App\Models\Entity\content_audit
 * @version July 25, 2018, 3:17 pm CST
 *
 * @property integer content_id
 * @property string content_model
 * @property string audit_type
 * @property integer audit1_status
 * @property integer audit1_userid
 * @property timestamp audit1_time
 * @property string audit1_text
 * @property integer audit2_status
 * @property integer audit2_userid
 * @property timestamp audit2_time
 * @property string audit2_text
 * @property integer audit3_status
 * @property integer audit3_userid
 * @property timestamp audit3_time
 * @property string audit3_text
 */
class content_audit extends Model
{
   // use SoftDeletes;


    public $table = 'content_audits';
    

    protected $dates = ['deleted_at'];


    public $fillable = [
        'content_id',
        'content_model',
        'audit_type',
        'userid',
        'audit1_status',
        'audit1_userid',
        'audit1_time',
        'audit1_text',
        'audit2_status',
        'audit2_userid',
        'audit2_time',
        'audit2_text',
        'audit3_status',
        'audit3_userid',
        'audit3_time',
        'audit3_text'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'content_id' => 'integer',
        'content_model' => 'string',
        'audit_type' => 'string',
        'userid' => 'integer',
        'audit1_status' => 'integer',
        'audit1_userid' => 'integer',
        'audit1_text' => 'string',
        'audit2_status' => 'integer',
        'audit2_userid' => 'integer',
        'audit2_text' => 'string',
        'audit3_status' => 'integer',
        'audit3_userid' => 'integer',
        'audit3_text' => 'string'
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'content_id' => 'required|integer',
        'content_model' => 'required|max:50',
        'audit_type' => 'required|imax:50'
    ];

    public static $audit_type = [
        'moderate'=>'编辑',
        'online'=>'上线',
        'publish'=>'发布',
        'offline'=>'下线',
    ];

    public static $content_model = array(
        'vasseti'=>'App\Models\Entity\Asset\Video\Vasseti',
        'vchan'=>'App\Models\Entity\Asset\Vlive\Vchan',
        'vseriali'=>'App\Models\Entity\Asset\Video\Vseriali',
        'iaasset'=>'App\Models\Entity\Asset\Iaasset',
        'iaseriali'=>'App\Models\Entity\Asset\Iaseriali',
    );

    public static $audit_status = array(
        '0'=>'未审核',
        '1'=>'审核中',
        '2'=>'审核不通过',
        '3'=>'审核通过'
    );

    public static function audit_status($content_id,$content_model,$audit_type,$audit_level=0){

        $audit_setting = json_decode(config('custom.config.CONTENT_AUDIT_SETTING'),true);
        if($audit_level==0){
            $audit_level = $audit_setting[$audit_type];
        }

        $where = [
            ['content_id','=',$content_id],
            ['content_model','=',$content_model],
            ['audit_type','=',$audit_type]
        ];
        $data = self::where($where)->orderBy('id', 'desc')->first();
        if(empty($data)){
            return 0;
        }
        switch ($audit_level) {
            case 1:
                if($data->audit1_status === null){
                    return 1;
                }elseif ($data->audit1_status == 0){
                    return 2;
                }elseif ($data->audit1_status == 1){
                    return 3;
                }
                break;
            case 2:
                if($data->audit2_status === null){
                    return 1;
                }elseif ($data->audit2_status == 0){
                    return 2;
                }elseif ($data->audit2_status == 1){
                    return 3;
                }
                break;
            case 3:
                if($data->audit3_status === null){
                    return 1;
                }elseif ($data->audit3_status == 0){
                    return 2;
                }elseif ($data->audit3_status == 1){
                    return 3;
                }
                break;
        }

    }

    /**
     * 获得。
     */
    public function vasseti()
    {
        return $this->belongsTo('App\Models\Entity\Asset\Video\Vasseti', 'content_id');
    }


    /**
     * 获取所有拥有的auditable模型
     */
    public function auditable()
    {
        return $this->morphTo();
    }


    
}
